package com.boray.test;

import java.io.IOException;
import java.io.Reader;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.boray.onlinestore.domain.entity.Supplier;
import com.boray.onlinestore.domain.entity.SupplierExample;
import com.boray.onlinestore.infrastructure.dao.SupplierMapper;


public class Main {
	
	static String resource = "myBatisConfig.xml"; 
	static Reader reader;
	static{
		try {
			reader = Resources.getResourceAsReader(resource);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	static SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader);
	static SqlSession sqlSession = sqlMapper.openSession();
	static SupplierExample supplierEx = new SupplierExample();
	static SupplierMapper mapper = sqlSession.getMapper(SupplierMapper.class); 
	
	public static void insert() throws IOException{
		Supplier record = new Supplier();
		record.setId(1001+"");
		record.setSupplierNo("1000001");
		record.setSupplierName("thinks");
		record.setState("ok");
		record.setCreateTime(new Date().toString());
		record.setUpdateTime(new Date().toString());
		//int i = mapper.insert(record);
		int i =mapper.insertSelective(record);
		System.out.println(i);
		
	}
	
	public static void update(){
		List<Supplier> allRecords = mapper.selectByExample(supplierEx);
		//allRecords.get(0).setUpdateTime(updateTime)
		//supplierEx.or().andSupplierNameIsNotNull();
		
		int i= mapper.updateByExampleSelective(allRecords.get(0), supplierEx);
		System.out.println(i);
	}
	
	public static void selectAll(){

		List<Supplier> allRecords = mapper.selectByExample(supplierEx); 
		System.out.println("allRecords.size: "+allRecords.size());
		for (Supplier s : allRecords) {
			System.out.println(s);
		}
		
	}
	
	public static void selectById(){
		supplierEx.or().andSupplierNameIsNotNull();
		List<Supplier> allRecords = mapper.selectByExample(supplierEx); 
		System.out.println("allRecords.size: "+allRecords.size());
		for (Supplier s : allRecords) {
			System.out.println(s);
		}
	}
	public static void selectByName(){
		supplierEx.or().andSupplierNameLike("%thi%");
		List<Supplier> allRecords = mapper.selectByExample(supplierEx);
		System.out.println("allRecords.size: "+allRecords.size());
		for (Supplier s : allRecords) {
			System.out.println(s);
		}
	}
	public static void main(String[] args) throws IOException {
		
		//pet.or().andDeathIsNotNull(); 
		
		
		//insert();
		//selectAll();
		//update();
		//selectById();
		selectByName();
		//sqlSession.commit();
		sqlSession.close();
		
	}

}
